<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm" :rules="rules" ref="addForm">
                    <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                        <el-input v-model="addForm.title" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="来源" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.origin" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="内容" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.content" auto-complete="off"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="是否隐藏" :label-width="formLabelWidth">
                        <el-switch v-model="show" active-color="#13ce66">
                        </el-switch>
                    </el-form-item> -->
                    <el-form-item label="视频" :label-width="formLabelWidth">
                        <el-upload class="upload-demo" 
                            action="" 
                            ref="upload" 
                            :http-request="videoUpload"
                            :on-preview="handlePreview" 
                            :on-remove="handleRemove" 
                            :limit="1" 
                            :on-exceed="handleExceed"
                            :file-list="fileList"
                            accept="video/*"
                        >
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入标题" class="search"></el-input>
            <!-- <el-date-picker v-model="time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> -->
            <!-- </el-date-picker> -->
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="addEx">新增视频</el-button>
        </div>
        <div>
            <el-table :data="videos" border fit style="width: 100%">
                <el-table-column label="视频">
                    <template slot-scope="scope">
                        <video :src="scope.row.url" controls width="100%" height="100%">
                            你的浏览器不支持
                            <code>video</code> 标签
                        </video>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="标题" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="content" label="内容" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="update_at" label="添加时间" :formatter="timeFarmatter">
                </el-table-column>
                <el-table-column prop="origin" label="来源">
                </el-table-column>
                <el-table-column prop="watch_times" label="观看次数">
                </el-table-column>
                <el-table-column prop="state" :formatter="formatStatus" label="状态">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editPic(scope.row)" size="small">编辑</el-button>
                        <el-button type="text" @click="delPic(scope.row)" size="small">删除</el-button>
                        <el-switch 
                            v-model="scope.row.state === 1 ? true : false" 
                            active-color="#13ce66"
                            @change="switchChange(scope.row)"
                        >
                        </el-switch>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" 
            :current-page="page.number" 
            layout="total, prev, pager, next, jumper" 
            :total="page.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { video, uploadFile } from '../../server/getDate.js'
import { mapState, mapActions } from 'vuex'
import { dateFormatter } from './../../utils/local.js'
const { listApi, addApi, updateApi, deleteApi, disableApi } = video
export default {
    data() {
        return {
            addForm: {},
            dialogFormVisible: false,
            dialogFormState: false,
            title: '',
            formLabelWidth: '120px',
            show: false,
            fileList: [],
            select_word: '',
            time: '',
            rules: {
                title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
            }
        }
    },
    created() {
        this.getAllVideos(1)
    },
    computed: {
        ...mapState({
            videos: state => state.File.videos,
            page: state => state.File.videsPage
        })
    },
    methods: {
        ...mapActions(['getVideos']),
        getAllVideos(number) {
            const params = {
                page: number,
                pageSize: 10
            }
            if (this.select_word !== '') {
                params.searchKey = this.select_word
            }
            listApi({ ...params }).then(res => {
                if (res.code === '000000') {
                    this.getVideos({
                        ...res,
                        number
                    })
                } else {
                    this.$message.error(res.message)
                }
            }, err => this.$message.error('服务器异常'))
        },
        add() {
            this.dialogFormVisible = false
            if (this.fileList && this.fileList.length > 0) {
                this.addForm.originName = this.fileList[0].name
                this.addForm.url = this.fileList[0].url
            }
            this.$refs['addForm'].validate(valid => {
                if (valid) {
                    if (this.addForm.id) {
                        updateApi({ ...this.addForm }).then(res => {
                            if (res.code === '000000') {
                                this.$message.success('修改成功')
                                this.getAllVideos(this.page.number)
                            } else {
                                this.$message.error(res.message)
                            }
                        }, () => {
                            this.$message.error('网络错误')
                        })
                    } else {
                        addApi({ ...this.addForm }).then(res => {
                            if (res.code === '000000') {
                                this.$message.success('添加成功')
                                this.getAllVideos(this.page.number)
                            } else {
                                this.$message.error(res.message)
                            }
                        }, () => {
                            this.$message.error('网络错误')
                        })
                    }
                }
            })
        },
        addEx() {
            this.dialogFormVisible = true
            this.title = '添加视频'
            this.addForm = {}
            this.fileList = []
        },
        editPic(row) {
            this.dialogFormVisible = true
            this.title = '修改视频'
            this.addForm = row
            this.fileList = []
            if (row.origin_name && row.url) {
                this.fileList.push({
                    name: row.origin_name,
                    url: row.url
                })
            }
        },
        delPic(row) {
            this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteApi({ ids: row.id }).then(res => {
                    if (res.code === '000000') {
                        this.$message.success('删除成功')
                        this.getAllVideos(this.page.number)
                    } else {
                        this.$message.error(err.message)
                    }
                }).catch(() => this.$message.error('网络错误'))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        formatStatus(row, col) {
            return row.state === 1 ? '展示' : "不展示"
        },
        handleCurrentChange(val) {
            this.getAllVideos(this.page.number)
        },
        handleExceed(files, filelist) {
            console.log('handleExceed', files, filelist)
        },
        handleRemove(file, filelist) {
            console.log('remove', file, filelist)
            this.fileList = filelist
        },
        handlePreview(file) {
            console.log('preview', file)
        },
        search() {
           this.getAllVideos(this.page.number)
        },
        videoUpload(value) {
            this.fileList = []
            const formData = new FormData()
            formData.append('fileName', value.file)
            formData.append('fileType', '3')
            uploadFile(formData, res => {
                if (res.code === '000000') {
                    this.fileList.push({
                        url: res.result.storageName,
                        name: res.result.originName
                    })
                }
            })
        },
        timeFarmatter(row) {
            return dateFormatter(row.update_at)
        },
        switchChange(row) {
            disableApi({
                ids: row.id,
                state: row.state ? '0' : '1'
            }).then(res => {
                if (res.code === '000000') {
                    this.$message.success('修改成功')
                    this.getAllVideos(this.page.number)
                } else {
                    this.$message.error(res.message)
                }
            }, () => {
                this.$message.error('网络错误')
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}
.block {
  text-align: center;
  margin-top: 1%;
}
</style>